#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>






#include "genserver.h"

#define TCP_PORT            (9734)
#define UDP_PORT            (19734)
#define HEADRSIZE       (4)
#define EXPTMESSIZE     (10)

int RI=0;

int numofclients;
int    flag =0;
typedef struct application_t
{
    GenSv*      m_sv;
}appl;

struct argum_t
{
    char     filename1[200];
    char     filename2[200];
    char     filename3[200];
    GenSv*   sv;
};

/*Global variables*********************************************************/
char** argv;
int    IDcont;
int    loop=0;
/*********CallBackFunction************************************************/
/* 1st 4 bytes in each meassage*/
size_t Headrfunc(void* _buf,int _idclient,void* _appdata)
{
    return *((size_t*) _buf);
}

void NewClient(int _idclient,void* _app)
{

    printf("\nNewClientCALLBACK , id : %d",_idclient);
    fflush(stdout);
    return;
}

int couplflag=0;
void NewMessage(int _IDclient,void* _app, void*  _buf,size_t _mesSize)
{
    char*  answer1={"\nSharikPriloharik\n"};
    char*  answer2={"\nKam Tsu Mir\n"};

     printf("\n Message recieved from\
     client ID %d  : %s \n", _IDclient, (char*)_buf);
     fflush(stdout);

     if(couplflag%2)
     {
         printf("\nServer send Message to client ID %d\n",_IDclient);
         GsvServerWrite(((appl*)_app)->m_sv,_IDclient,answer1,strlen(answer1)+1);
     }
     else
     {
         printf("\nServer send 3 Messages to client ID %d\n",_IDclient);
         GsvServerWrite(((appl*)_app)->m_sv,_IDclient,answer2,strlen(answer2)+1);
        /* GsvServerWrite(((appl*)_app)->m_sv,_IDclient,answer2,strlen(answer2)+1);
         GsvServerWrite(((appl*)_app)->m_sv,_IDclient,answer2,strlen(answer2)+1);*/
     }
        if( couplflag > 40000)
        {
            printf("\n!!!!Server ORDER to disconnect the user with ID  %d  :\n!!!!!",_IDclient);
            GsvServerDrop(((appl*)_app)->m_sv,_IDclient);
        }
        ++couplflag;
    return;
}

void NewExeption(int _IDclient,void* _app, void*  _buf,size_t _mesSize)
{
    printf("Exeption message arrived from client\
    ID %d was %s :\n",_IDclient, (char*)_buf);
}

void ClientDiscn(int _IDclient,void* _app,size_t _notreaded)
{
    printf("\n Problem with client ID %d ,\
      \n%d bytes remain _not readed ", _IDclient,_notreaded);
     return;
}
/******************************************************************************/

void regression_test();
int  unitest_SimplemMultyMessage();

int main(int _argc, char* _argv[])
{
    argv = (char**)_argv;/*initillize the global const*/
    printf( "\nargument number are : %d " ,  _argc);
    while(_argc--)
    {
        printf( "\nthe argements are : %s ", *((_argv)++));
    }
    regression_test();
    return 0;
}

void regression_test()
{
    unitest_SimplemMultyMessage();

}

int unitest_SimplemMultyMessage()
{
    appl*       app=(appl*)malloc(sizeof(appl));
    GenSv*      sv = GsvServerNew(TCP_PORT,UDP_PORT,app,HEADRSIZE,
        Headrfunc,NewClient,NewMessage,ClientDiscn,
        EXPTMESSIZE,NewExeption);
    app->m_sv = sv;
    GsvServerRun(sv);
    return 0;
}





